﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;
using Net.Chirash.Twitter.TwitterDatabase.Statistics;

namespace Net.Chirash.Twitter.TweetZone
{
    public enum TimelineTypes
    {
        Empty,
        Home,
        Mentions,
        DirectMessage,
        User,
        Other,
    }

    public enum UserListType
    {
        Friend,
        Follower
    }

    /// <summary>
    /// 与えられたITwitterCommandと列挙体からTwitterDataを生成するヘルパークラス。
    /// </summary>
    public class TwitterDataFactory
    {

        public TwitterDataFactory()
        {
        }

        public static Timeline GetTimeline(TimelineOption option, TimelineTypes type, ITwitterCommand command)
        {
            Timeline timeline;
            switch (type)
            {
                case TimelineTypes.Home:
                    timeline = command.GetHomeTimeline(option);
                    break;
                case TimelineTypes.Mentions:
                    timeline = command.GetMentionsTimeline(option);
                    break;
                case TimelineTypes.User:
                    //option.UserID = option.UserID;
                    timeline = command.GetUserTimeline(option);
                    break;
                default:
                    timeline = command.GetHomeTimeline(option);
                    break;
            }

            return timeline;
        }

        public static DirectMessageCollection GetDirectMessages(DirectMessageOption option, ITwitterCommand command)
        {
            DirectMessageCollection result;
            result = command.GetDirectMessages(option);
            return result;
        }

        public static UserList GetUserList(UserListOption option, UserListType type, ITwitterCommand command)
        {
            UserList list;
            switch (type)
            {
                case UserListType.Follower:
                    list = command.Followers(option);
                    break;
                case UserListType.Friend:
                    list = command.Friends(option);
                    break;
                default:
                    list = new UserList();
                    break;
            }
            return list;
        }
    }

    /// <summary>
    /// 現在の設定から適切なTwitterCommandを生成するクラス
    /// </summary>
    public class TwitterCommandFactory
    {
        private static TwitterCachingConnection _cachingConn;
        private static TwitterCachingConnection cachingConn
        {
            get
            {
                if (_cachingConn == null)
                {
                    _cachingConn = new TwitterCachingConnection(Net.Chirash.Twitter.TweetZone.Properties.Settings.Default.TwitterConnectionString);
                }
                return _cachingConn;
            }
        }

        /// <summary>
        /// 現在利用可能なITwitterCommandを返します
        /// </summary>
        /// <returns></returns>
        // TODO : 設定の実装
        public static ITwitterCommand CreateTwitterCommand()
        {
            ITwitterCommand icmd;
            var caching = new TwitterLocalCommand(cachingConn, App.CurrentApp.User);
            //caching.Open();
            icmd = caching;

            return icmd;
        }

        /// <summary>
        /// ローカルで利用可能なITwitterCommandを返します。
        /// </summary>
        /// <returns></returns>
        public static TwitterLocalCommand CreateLocalTwitterCommand()
        {
            return CreateTwitterCommand() as TwitterLocalCommand; //temporalily
        }

        public static TwitterStatistics CreateTwitterStatistics()
        {
            return new TwitterStatistics(cachingConn);
        }

        /// <summary>
        /// リモートで利用可能なITwitterCommandを返します。
        /// </summary>
        /// <returns></returns>
        public static TwitterCommand CreateRemoteTwitterCommand()
        {
            return new TwitterCommand(App.CurrentApp.Connection);
        }
    }

}
